#include "image.hpp"
#include "core/assert.hpp"
#include "imgui.h"
#include "imgui_ext/texture.h"


void image_view::update_data (bindable_base *data)
{
    data_->attach (data);
    width_->attach (data);
    height_->attach (data);
}

void image_view::init_content (const xml_node &node)
{
    data_   = create_data (node.attribute ("data").as_string (), image);
    width_  = create_data (node.attribute ("width").as_string (), unsigned_integral);
    height_ = create_data (node.attribute ("height").as_string (), unsigned_integral);

    PI_ASSERT_MSG (data_ && height_ && width_, "image requires data,width,height attributes!!");
}

void image_view::draw_self ()
{
    ImageContent *ic = data_->get_image ();
    if (ic)
    {
        u64  w   = width_->get_u64 ();
        u64  h   = height_->get_u64 ();
        auto tex = (Texture *) ic->handle ();
        ImGui::Image ((ImTextureID) (intptr_t) tex->Id (), ImVec2 (w, h));
    }
}
